package com.hzdp.sort;

/**
 * i-j+，相邻对比,最好O(n)，平均O(n^2)
 * 
 * @author liuming
 * 
 */
public class BubbleSort {

	public static void bubbleSort(int[] array) {
		for (int i = array.length - 1; i > 0; i--) {
			boolean flag = true;
			for (int j = 0; j < i; j++) {
				if (array[j] > array[j + 1]) {
					int temp = array[j + 1];
					array[j + 1] = array[j];
					array[j] = temp;
					flag = false;
				}
			}
			if (flag) {
				break;
			}
		}
	}

	public static void main(String[] args) {
		int[] array = { 2, 32, 10, 0, 2, 11, 30, 2, 4, 5, 1, 2 };
		bubbleSort(array);
		for (int i : array) {
			System.out.print(i + ",");
		}
	}
}
